package com.brillilab.starter.controller.aio.account;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.brillilab.common.constant.SystemConfigConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.account.IntegralStateEnum;
import com.brillilab.domain.po.account.IntegralRecord;
import com.brillilab.domain.po.account.UserAccount;
import com.brillilab.domain.po.system.SystemConfig;
import com.brillilab.domain.vo.account.AccountDetailedVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.account.IntegralRecordService;
import com.brillilab.service.core.system.ISystemConfigService;
import com.brillilab.service.logic.account.ExchangeRecordLogic;
import com.brillilab.service.logic.account.UserAccountLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;

/**
 * @author LJ
 * @description
 */
@RestController
@RequestMapping("/api/account")
public class UserAccountController {

    @Resource
    private ExchangeRecordLogic exchangeRecordLogic;
    @Resource
    private UserAccountLogic userAccountLogic;
    @Resource
	private IntegralRecordService integralRecordService;
    @Resource
    private UsersLogic usersLogic;
    @Resource
    private ISystemConfigService systemConfigService;

    /**
     * 用户账户信息
     * @param token
     * @return
     */
    @GetMapping("/info")
    public ResponseVo accountInfo(@RequestHeader(value= StarterConstant.TOKEN)String token) {
        UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
        UserAccount userAccount = userAccountLogic.getByUserId(userInfoVo.getId());
        return ResponseVo.success(userAccount);
    }
    
 
    /**
     * 积分兑换码
     * @param token
     * @return
     */
    @GetMapping("/exchangeCode")
    public ResponseVo exchangeCode(@RequestHeader(value= StarterConstant.TOKEN)String token) {
        UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
        Map<String, Object> map = new HashMap<>();
        map.put("exchangeCode", exchangeRecordLogic.withdraw(userInfoVo));
        SystemConfig systemConfig = systemConfigService.selectByCode(SystemConfigConstant.RE_KF);
        if(systemConfig!=null && StringUtils.isNotBlank(systemConfig.getValue())) {
        	JSONObject jsonObject = JSON.parseObject(systemConfig.getValue());
        	map.put("weChat", jsonObject.get("wxAccount"));
        }
        return ResponseVo.success(map);
    }
    
    
    /**
     * 账户明细
     * @param token
     * @return
     */
    @GetMapping("/detailed")
    public ResponseVo accountDetailed(@RequestHeader(value= StarterConstant.TOKEN)String token,
    		int pageNum, int pageSize) {
        UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
        PageVo<AccountDetailedVo> voList = userAccountLogic.getAccountDetailed(userInfoVo.getId(),pageNum,pageSize);
        return ResponseVo.success(voList);
    }
    
    /**
     * 获取红包列表
     * @param token
     * @return
     */
    @GetMapping("/redPackageList")
    public ResponseVo redPackageList(@RequestHeader(value= StarterConstant.TOKEN)String token) {
        UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
        UserAccount userAccount = userAccountLogic.getByUserId(userInfoVo.getId());
        List<IntegralRecord> recordList = userAccountLogic.getIntegralRecord(userInfoVo.getId(),IntegralStateEnum.UNRECEIVED);
        Map<String, Object> map = new HashMap<>();
        map.put("totalIntegral", userAccount.getTotalIntegral());
        map.put("recordList", recordList);
        return ResponseVo.success(map);
    }
    
    /**
     * 获取单个红包
     * @param token
     * @return
     */
    @GetMapping("/redPackage")
    public ResponseVo redPackage(@RequestHeader(value= StarterConstant.TOKEN)String token,
    		Long recordId) {
        UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
        UserAccount userAccount = userAccountLogic.getByUserId(userInfoVo.getId());
        IntegralRecord integralRecord = integralRecordService.getRecord(userInfoVo.getId(), recordId);
        Map<String, Object> map = new HashMap<>();
        map.put("totalIntegral", userAccount.getTotalIntegral());
        map.put("integralRecord", integralRecord);
        return ResponseVo.success(map);
    }
   
    /**
     * 用户领取红包
     * @param token
     * @return 领取状态：1领取成功，2已过期，3已被领取过
     */
    @GetMapping("/receivedIntegral")
    public ResponseVo receivedIntegral(@RequestHeader(value= StarterConstant.TOKEN)String token, 
    		@RequestParam  Long recordId) {
        UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
        int state = userAccountLogic.receivedIntegral(userInfoVo.getId(),recordId); 
        UserAccount userAccount = userAccountLogic.getByUserId(userInfoVo.getId());
        Map<String, Object> map = new HashMap<>();
		map.put("state", state);
		map.put("totalIntegral", userAccount.getTotalIntegral());
        return ResponseVo.success(map);
    }
    
}
